#include <iostream>
#include <memory>
#include "udpserver.hpp"
#include "routle.hpp"
#include "threadpool.hpp"

using task_t = std::function<void()>;

int main(int argc, char *argv[])
{
    // 1.判断输入参数
    if (argc != 2)
    {
        Local_Log::LOG(Local_Log::LogLever::LFATAL) << "argc error";
        std::cout << "Usage " << argv[0] << "Port" << std::endl;
        exit(1);
    }
    // 2.解析参数
    // char server_ip[17];
    // memset((void *)server_ip, 0, sizeof(server_ip));
    // memcpy((void *)server_ip, argv[1], strlen(argv[1]));
    uint16_t server_posr = std::stoi(argv[1]);
    // 3.创建聊天室
    Local_Routle::Routle rout;
    // 4.创建线程池
    auto tp = Local_Thread_Pool::LocalThreadPool<task_t>::GetInstance();
    //  5.创建参数
    //   用智能指针管理
    std::unique_ptr<Udp_Server::UdpServer> use_server = std::make_unique<Udp_Server::UdpServer>(server_posr, [&rout, &tp]
            (int sockfd, const std::string &str, Local_Inetaddr::Inetaddr &input)
            {  
                task_t t=std::bind(&Local_Routle::Routle::Forward,&rout,sockfd,str,input);
                tp->TaskPush(t); 
            });
    // std::cout << "对象创建成功" << std::endl;

    // 初始化
    use_server->Init();
    // std::cout << "对象初始化成功" << std::endl;

    // 启动
    use_server->Start();

    return 0;
}